package jsonToolCompare.code;

import org.codehaus.jackson.map.ObjectMapper;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;

import jsonToolCompare.domain.Group;
import jsonToolCompare.domain.User;
import jsonToolCompare.domain.User2;

public class TestAllJsonlib {

    public TestAllJsonlib() {
        buildJsonString();
    }

    private void buildJsonString() {
        Group group = new Group();

        for (int i = 0; i < 2000000; i++) {
            User user = new User();

            user.setId(1L);
            user.setName("test" + i);
            user.setVendor("class" + i);
            user.setImageUrl("img/class.png" + i);
            user.setCreator("niles" + i);
            user.setInfo("Test user in group" + i);
            user.setUser(JSON.parseObject(JSON.toJSONString(user), User.class));
            user.setUser2(JSON.parseObject(JSON.toJSONString(user), User2.class));
            group.addUser(user);

        }
        try {
            System.err.println("start compare...");
            Gson gson = new Gson();

            ObjectMapper mapper = new ObjectMapper();

            long t0 = 0;
            long t1 = System.currentTimeMillis();
            String str = null;

            t0 = t1;
            str = mapper.writeValueAsString(group);
            t1 = System.currentTimeMillis();
            System.err.println("jackson serialize object:" + (t1 - t0));

            t0 = t1;
            str = JSON.toJSONString(group);
            t1 = System.currentTimeMillis();
            System.err.println("fastjson serialize object:" + (t1 - t0));

            t0 = t1;
            str = gson.toJson(group);
            t1 = System.currentTimeMillis();
            System.err.println("Gson serialize object:" + (t1 - t0));

            System.err.println("--------------");

            t0 = t1;
            Group group2 = mapper.readValue(str, Group.class);
            t1 = System.currentTimeMillis();
            System.err.println("jackson parse object:" + (t1 - t0));

            t0 = t1;
            Group group0 = JSON.parseObject(str, Group.class);
            t1 = System.currentTimeMillis();
            System.err.println("fastjson parse object:" + (t1 - t0));

            t0 = t1;
            Group group1 = gson.fromJson(str, Group.class);
            t1 = System.currentTimeMillis();
            System.err.println("Gson parse object:" + (t1 - t0));

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new TestAllJsonlib();
    }

}